package com.lej.dataservice.club.http

import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.lej.core.base.BaseApplication.Companion.instance
import com.lej.core.base.app
import com.lej.core.utils.extention.versionCode
import com.lej.dataservice.club.bean.TipsBean
import com.lej.dataservice.club.bean.ad.HomeAdInfo
import com.lej.dataservice.club.bean.commercial.CommercialRes
import com.lej.dataservice.club.bean.room.RoomListInfo
import com.lej.dataservice.club.bean.share.ShareCodeAnalyzeBean
import com.lej.dataservice.club.bean.upgrade.UpgradeInfo
import com.lej.dataservice.club.bean.upload.PublicNumInfo
import com.lej.dataservice.club.http.RetrofitClient.baseUrl
import com.lej.stethomodule.StethoUtils
import okhttp3.ConnectionPool
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.http.*
import java.util.concurrent.TimeUnit


val baseApiService: BaseApiService = RetrofitClient.getInstance().create(BaseApiService::class.java)

interface BaseApiService {
    companion object {
        ///不添加默认参数，只是getUrl 下载用（避免改变url导致的gsyVideo 代理下载问题）
        val downloadService: BaseApiService
            get() = kotlin.run {
                val sslParams = HttpsUtils.getSslSocketFactory()
                val builder = OkHttpClient.Builder()
                    .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)

                if (instance.isDebug) builder.addNetworkInterceptor(StethoUtils.configureInterceptor())
                val okHttpClient = builder.connectTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .connectionPool(
                        ConnectionPool(8, 15, TimeUnit.SECONDS)
                    ) // 这里你可以根据自己的机型设置同时连接的个数和时间，我这里8个，和每个保持时间为10s
                    .build()
                val retrofit = Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(baseUrl)
                    .build()
                retrofit.create(BaseApiService::class.java)
            }
    }

    //region ====================================common====================================
    /**
     * 获取七牛token
     */
    @GET("api/qiniutoken")
    suspend fun qnToken(): JsonObject

    //获取客服微信公众号
    @GET("api/kefuinfo")
    suspend fun kefuinfo(): BaseResponse<PublicNumInfo?>?

    //app内提示文案
    @GET("api/toastconf")
    suspend fun toastconf(): BaseResponse<HashMap<String, TipsBean>>?

    //推送绑定 [maker] 厂商
    @FormUrlEncoded
    @POST("trove/usersEquipment/save")
    suspend fun bindingCid(
        @Field("number") uuid: String?,
        @Field("type") maker: String?
    ): BaseResponse<JsonElement>

    /**获取更新信息*/
    @GET("trove/client/appversion")
    suspend fun getVersion(
        @Query("version_code") version_code: Long,
        @Query("model") cate: Int = 0
    ): BaseResponse<UpgradeInfo>

    /**开屏广告*/
    @GET("api/floatad")
    suspend fun floatad(): BaseResponse<HomeAdInfo>
    //endregion =====================================common=====================================

    //region =====================================分享相关====================================
    /**口令换取分享具体内容*/
    @GET("api/exchangecodeinfo")
    suspend fun exchangecodeinfo(@Query("share_code") share_code: String): BaseResponse<ShareCodeAnalyzeBean>
    /*endregion ==================================分享相关=============================== */

    //region ============================用户信息====================================
    @GET("trove/nfthouse/mine")
    suspend fun MyRoomList(): BaseResponse<RoomListInfo>
    //endregion =========================用户信息====================================

    //开屏广告列表
    @GET("api/startconf")
    suspend fun liststartad(): BaseResponse<CommercialRes>

    @GET
    fun get(@Url url: String?): Call<ResponseBody>

    @POST
    @FormUrlEncoded
    fun post(@Url url: String?, @FieldMap hashMap: HashMap<String, String>?): Call<ResponseBody>
}